package com.lg.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.SUser;
import com.lg.erp.mapper.UserMapper;
import com.lg.erp.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ligen
 * @since 2021-04-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SUser> implements IUserService {

    @Autowired
    private IUserService userService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public SUser getUserByLogin() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SUser one=new SUser();
        if(principal instanceof org.springframework.security.core.userdetails.User &&principal!=null) {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal;
            String username = user.getUsername();
             one = userService.getOne(new QueryWrapper<SUser>()
                    .lambda()
                     //必须额外著名是哪个User否则默认使用调用的user
                     //没法识别
                    .eq(com.lg.erp.entity.SUser::getUsername, username));
        }
        return one;
    }

    @Override
    public IPage<SUser> getUserByPage(Page<SUser> page) {
        return userMapper.selectUserByPage(page);
    }
}
